package com.zzx.controller;

import com.github.pagehelper.PageInfo;
import com.zzx.bean.Bill;
import com.zzx.bean.BillType;
import com.zzx.service.BillService;
import com.zzx.service.BillTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Priority;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author: Zahi
 * @Description:
 * @Date Created in 2020-10-22 16:12
 * @Modified By:
 */
@Controller
@RequestMapping("/bill")
public class BillController {

    @Resource
    private BillService service;

    @Resource
    private BillTypeService typeService;

    @RequestMapping("/list.do")
    public String list(Bill bill, Model model){
        List<BillType> types = typeService.list();
        model.addAttribute("types",types);
        List<Bill> list = service.list(bill);
        model.addAttribute("list",list);
        model.addAttribute("bill",bill);
        return "/bill/list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        List<BillType> types = typeService.list();
        model.addAttribute("types",types);
        return "/bill/add";
    }

    @RequestMapping("/add.do")
    public String add(Bill bill){
        service.add(bill);
        return "redirect:/bill/list.do";
    }

    @RequestMapping("/toUpdate/{id}")
    public String toUpdate(Model model, @PathVariable("id")Long id){
        List<BillType> types = typeService.list();
        model.addAttribute("types",types);
        Bill bill = service.get(id);
        model.addAttribute("bill",bill);
        return "/bill/update";
    }

    @RequestMapping("/update.do")
    public String update(Bill bill){
        service.update(bill);
        return "redirect:/bill/list.do";
    }

    @RequestMapping("/delete.do/{id}")
    public void delete(@PathVariable("id")Long id, HttpServletResponse response){
        int delete = service.delete(id);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (delete > 0){
            assert writer != null;
            writer.print("<script>alert('删除成功');location.href='/bill/list.do'</script>");
            return;
        }
        assert writer != null;
        writer.print("<script>alert('删除失败');location.href='/bill/list.do'</script>");
    }

    @RequestMapping("/list-page.do")
    public String listPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            Bill b,
            Model model) {
        List<BillType> types = typeService.list();
        model.addAttribute("types", types);
        PageInfo<Bill> pageInfo = service.listPage(b, pageNum, pageSize);
        model.addAttribute("page", pageInfo);
        model.addAttribute("bill", b);
        return "/bill/list-page";
    }
}
